package com.juyu.miliao.api.dao.redis;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.juyu.miliao.api.dao.mysql.FindDynamicLikeDao;
import com.juyu.miliao.api.domain.DynamicLikeInfo;
import com.juyu.miliao.api.domain.FindDynamicLikeInfo;
import com.juyu.miliao.common.constant.FindDynamicInfoRedisKey;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author xuanqicheng
 */
@Repository
public class FindDynamicLikeDaoRedis {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private FindDynamicLikeDao findDynamicLikeDao;


    /**
     * 根据findId与userId查看用户是否点赞次发现动态
     *
     * @param findId
     * @param userId
     * @return
     */
    public FindDynamicLikeInfo getFindDynamicInfoByFindIdAndUserId(int findId, long userId) throws IOException {
        String key = FindDynamicInfoRedisKey.REDIS_KEY.getKeyFormat(findId, userId);

        String target = stringRedisTemplate.opsForValue().get(key);
        if (StringUtils.isNotBlank(target)) {
            FindDynamicLikeInfo findDynamicLikeInfo = objectMapper.readValue(target, FindDynamicLikeInfo.class);
            return findDynamicLikeInfo;
        } else {
            FindDynamicLikeInfo query = new FindDynamicLikeInfo();
            query.setFindId(findId);
            query.setUserId(userId);
            FindDynamicLikeInfo findDynamicLikeInfo = findDynamicLikeDao
                    .selectInfoByUserIdAndFindId(query);
            if (findDynamicLikeInfo != null) {
                stringRedisTemplate.opsForValue().set(key, objectMapper.writeValueAsString(findDynamicLikeInfo), 10,
                        TimeUnit.MINUTES);
                return findDynamicLikeInfo;
            } else {
                return null;
            }
        }
    }


    /**
     * 查看是否点赞发现
     *
     * @param findId
     * @param userId
     * @return
     */
    public boolean checkFindDynamicInfoByFindIdAndUserId(int findId, long userId) throws IOException {

        FindDynamicLikeInfo obj = getFindDynamicInfoByFindIdAndUserId(findId, userId);
        if (obj == null) {
            return false;
        } else {
            return true;
        }
    }


    public int deleteDynamicLike(FindDynamicLikeInfo findDynamicLikeInfo) {
        int num = findDynamicLikeDao.deleteDynamicLike(findDynamicLikeInfo);
        deleteLikeCache(findDynamicLikeInfo.getFindId(),findDynamicLikeInfo.getUserId());
        return num;
    }

    

    private void deleteLikeCache(int findId, long userId) {
        String key = FindDynamicInfoRedisKey.REDIS_KEY.getKeyFormat(findId, userId);
        stringRedisTemplate.delete(key);
    }

    public int addFindDynamicLike(FindDynamicLikeInfo query) {
        int num = findDynamicLikeDao.addFindDynamicLike(query);
        deleteLikeCache(query.getFindId(),query.getUserId());
        return num;
    }
}
